package com.mukefile.action;

import com.mukefile.pojo.Manager;
import com.mukefile.pojo.ShareFile;
import com.mukefile.service.ShareFileService;
import com.mukefile.utils.GetPathProperties;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.io.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author: kenewstar
 * @Description:
 * @Date:Created in 2020/5/7
 */
@Controller("shareFileAction")
@Scope("prototype")
public class ShareFileAction extends ActionSupport implements ModelDriven<ShareFile> {

    @Resource
    private ShareFileService shareFileService;
    /**
     * 获取Request
     */
    private Map request = (Map) ActionContext.getContext().get("request");
    /**
     * 获取Session
     */
    private Map session = ActionContext.getContext().getSession();
    private ShareFile shareFile = new ShareFile();

    @Override
    public ShareFile getModel() {
        return this.shareFile;
    }

    /**
     * 显示所有共享
     *
     * @return
     */
    public String showAllShareFile() {

        List<ShareFile> list = this.shareFileService.showAllShareFile();
        if (list.isEmpty()) {
            //没有文件
            this.request.put("msg", "共享空间中暂无文件");
        }
        this.request.put("list", list);
        return "showAllShareFile";
    }

    /**
     * 删除共享文件
     *
     * @return
     */
    public String deleteFile() {
        Manager manager = (Manager)session.get("manager");
        int i = this.shareFileService.deleteFile(this.shareFile.getId(), this.shareFile.getFileName()
            ,manager);
        if (i == 0) {
            //删除失败
            this.request.put("msg", "文件删除失败");
        }
        this.session.put("list", this.shareFileService.showAllShareFile());
        this.request.put("msg", "文件删除成功");
        return "showAllShareFile";
    }

    private File file;
    private String fileFileName;

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public String getFileFileName() {
        return fileFileName;
    }

    public void setFileFileName(String fileFileName) {
        this.fileFileName = fileFileName;
    }

    /**
     * 上传文件至共享空间
     *
     * @return
     */
    public String fileUpload() {
        Manager manager = (Manager)session.get("manager");

        ShareFile shareFile = this.shareFile;
        shareFile.setFileName(this.fileFileName);
        shareFile.setFileSize((double) this.file.length());
        shareFile.setUploadTime(new Date());
        shareFile.setUploadName(manager.getName());
        int i = this.shareFileService.fileUpload(shareFile, this.file,manager);
        if (i == 0) {
            //上传失败
            this.request.put("msg", "文件上传失败");
        } else {
            this.request.put("msg", "文件上传成功");
        }
        this.request.put("list", this.shareFileService.showAllShareFile());
        return "showAllShareFile";
    }

    /**
     * 显示共享文件下载页面
     *
     * @return
     */
    public String fileSharing() {

        List<ShareFile> list = new ArrayList<>();
        list = this.shareFileService.showAllShareFile();
        this.request.put("list", list);
        return "fileSharing";
    }

    @Autowired
    private GetPathProperties getPathProperties;
    private String filename;

    public String getFilename() {
        return filename;
    }

    public void setFilename(String filename) {
        this.filename = filename;
    }

    private InputStream inputStream;

    public InputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    /**
     * 下载共享文件
     *
     * @return
     * @throws Exception
     */
    public String download() throws Exception {


        String path = getPathProperties.getUploadPath() + File.separator;
        inputStream =new FileInputStream(path+filename);
        filename = new String(filename.getBytes(),"ISO8859-1");
        return SUCCESS;
    }

}